"""
强化学习能力演示
展示如何使用反馈驱动的强化学习服务、自适应模型管理和智能A/B测试
"""

import sys
import os
import asyncio

# 添加项目根目录到Python路径
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

from src.services.feedback_reinforcement_service import get_feedback_driven_rl_service
from src.research_core.adaptive_model_manager import get_adaptive_model_manager
from src.research_core.intelligent_ab_testing import get_intelligent_ab_tester
from src.research_core.model_manager import get_model_manager, ModelType


async def demo_feedback_driven_rl():
    """演示反馈驱动的强化学习功能"""
    print("=== 反馈驱动的强化学习演示 ===")
    
    # 获取服务实例
    rl_service = get_feedback_driven_rl_service()
    
    # 模拟一些反馈数据
    feedback_data = [
        {
            "feedback_type": "performance",
            "priority": "high",
            "processed": False,
            "timestamp": 1640995200  # 某个时间戳
        },
        {
            "feedback_type": "error",
            "priority": "critical",
            "processed": False,
            "timestamp": 1640995300
        },
        {
            "feedback_type": "suggestion",
            "priority": "medium",
            "processed": False,
            "timestamp": 1640995400
        }
    ]
    
    # 处理反馈数据
    for feedback in feedback_data:
        reward = rl_service.convert_feedback_to_reward(feedback)
        print(f"反馈类型: {feedback['feedback_type']}, 优先级: {feedback['priority']}, 奖励值: {reward}")
    
    print("反馈处理完成\n")


def demo_adaptive_model_selection():
    """演示自适应模型选择功能"""
    print("=== 自适应模型选择演示 ===")
    
    # 获取服务实例
    model_manager = get_model_manager()
    adaptive_manager = get_adaptive_model_manager()
    
    # 模拟不同的任务场景
    scenarios = [
        {
            "task_type": "chat",
            "context": {
                "complexity": "high",
                "domain": "technical"
            }
        },
        {
            "task_type": "code_generation",
            "context": {
                "complexity": "medium",
                "domain": "software"
            }
        }
    ]
    
    for scenario in scenarios:
        task_type = scenario["task_type"]
        context = scenario["context"]
        
        # 使用自适应管理器选择模型
        model = adaptive_manager.select_optimal_model(task_type, context)
        print(f"任务类型: {task_type}")
        print(f"上下文: {context}")
        print(f"选择的模型: {model.__class__.__name__ if model else 'None'}")
        
        # 更新模型性能（模拟）
        adaptive_manager.update_model_performance(task_type, "TestModel", 0.85)
    
    print("自适应模型选择演示完成\n")


async def demo_intelligent_ab_testing():
    """演示智能A/B测试功能"""
    print("=== 智能A/B测试演示 ===")
    
    # 获取服务实例
    ab_tester = get_intelligent_ab_tester()
    
    # 定义提示词变体
    prompt_variants = [
        "请详细回答以下问题: {question}",
        "请简洁回答以下问题: {question}",
        "请以专业角度回答以下问题: {question}"
    ]
    
    # 定义测试用例
    test_cases = [
        {
            "input": {"question": "什么是人工智能?"},
            "expected_output": "人工智能是计算机科学的一个分支..."
        },
        {
            "input": {"question": "如何学习编程?"},
            "expected_output": "学习编程需要掌握基础语法..."
        }
    ]
    
    # 定义上下文
    context = {
        "domain": "education",
        "user_level": "beginner"
    }
    
    # 运行智能A/B测试
    print("运行智能A/B测试...")
    results = await ab_tester.run_intelligent_ab_test(prompt_variants, test_cases, context)
    
    # 显示结果
    print("A/B测试结果:")
    for variant_name, variant_data in results.get("variants", {}).items():
        print(f"  {variant_name}: 总体得分 = {variant_data.get('overall_score', 0)}")
    
    best_variant = results.get("best_variant", {})
    print(f"最佳变体: {best_variant}")
    
    # 自动优化提示词
    base_prompt = "请回答以下问题: {question}"
    optimized_prompt = ab_tester.auto_optimize_prompts(base_prompt, results)
    print(f"优化后的提示词: {optimized_prompt}")
    
    print("智能A/B测试演示完成\n")


async def main():
    """主函数"""
    print("强化学习能力演示开始\n")
    
    # 运行反馈驱动的强化学习演示
    await demo_feedback_driven_rl()
    
    # 运行自适应模型选择演示
    demo_adaptive_model_selection()
    
    # 运行智能A/B测试演示
    await demo_intelligent_ab_testing()
    
    print("所有演示完成")


if __name__ == "__main__":
    asyncio.run(main())